package com.ruoyi.zsyh.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.mapper.SysDtAreaMapper;
import com.ruoyi.zsyh.mapper.ZsyhAgentAreaMapper;
import com.ruoyi.zsyh.mapper.ZsyhUserRepairstoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zsyh.mapper.ZsyhAgentInfoMapper;
import com.ruoyi.zsyh.service.IZsyhAgentInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 大区会长档案Service业务层处理
 * 
 * @author yuexl
 * @date 2021-12-10
 */
@Service
public class ZsyhAgentInfoServiceImpl implements IZsyhAgentInfoService 
{
    @Autowired
    private ZsyhAgentInfoMapper zsyhAgentInfoMapper;
    @Autowired
    private SysDtAreaMapper sysDtAreaMapper;
    @Autowired
    private ZsyhAgentAreaMapper zsyhAgentAreaMapper;
    @Autowired
    private ZsyhUserRepairstoreMapper zsyhUserRepairstoreMapper;

    /**
     * 查询大区会长档案
     * 
     * @param id 大区会长档案ID
     * @return 大区会长档案
     */
    @Override
    public ZsyhAgentInfo selectZsyhAgentInfoById(String id)
    {
        return zsyhAgentInfoMapper.selectZsyhAgentInfoById(id);
    }

    public ZsyhAgentInfo selectZsyhAgentInfoByPhone(String agentPhone){
        return zsyhAgentInfoMapper.selectZsyhAgentInfoByPhone(agentPhone);
    }

    /**
     * 查询大区会长档案列表
     * 
     * @param zsyhAgentInfo 大区会长档案
     * @return 大区会长档案
     */
    @Override
    public List<ZsyhAgentInfo> selectZsyhAgentInfoList(ZsyhAgentInfo zsyhAgentInfo)
    {
        return zsyhAgentInfoMapper.selectZsyhAgentInfoList(zsyhAgentInfo);
    }

    /**
     * 新增大区会长档案
     * 
     * @param zsyhAgentInfo 大区会长档案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZsyhAgentInfo(ZsyhAgentInfo zsyhAgentInfo)
    {
        zsyhAgentInfo.setId(IdWorker.getId());
        zsyhAgentInfo.setCreateTime(DateUtils.getNowDate());
        int rows = zsyhAgentInfoMapper.insertZsyhAgentInfo(zsyhAgentInfo);
        insertZsyhAgentArea(zsyhAgentInfo);
        return rows;
    }

    /**
     * 修改大区会长档案
     * 
     * @param zsyhAgentInfo 大区会长档案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhAgentInfo(ZsyhAgentInfo zsyhAgentInfo)
    {
        zsyhAgentInfo.setUpdateTime(DateUtils.getNowDate());
        //回写区域问题
        ZsyhAgentArea zsyhAgentAreaNew = new ZsyhAgentArea();
        zsyhAgentAreaNew.setAgentId(zsyhAgentInfo.getId());
        List<ZsyhAgentArea> zsyhAgentAreaList = zsyhAgentAreaMapper.selectZsyhAgentAreaList(zsyhAgentAreaNew);
        rebackSysDtArea(zsyhAgentAreaList);
        zsyhAgentInfoMapper.deleteZsyhAgentAreaByAgentId(zsyhAgentInfo.getId());
        insertZsyhAgentArea(zsyhAgentInfo);
        return zsyhAgentInfoMapper.updateZsyhAgentInfo(zsyhAgentInfo);
    }

    @Override
    public int updateZsyhAgentInfoApplet(ZsyhAgentInfo zsyhAgentInfo)
    {
        zsyhAgentInfo.setUpdateTime(DateUtils.getNowDate());
        return zsyhAgentInfoMapper.updateZsyhAgentInfo(zsyhAgentInfo);
    }

    /**
     * 删除大区会长档案对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZsyhAgentInfoByIds(String ids)
    {
        //先恢复区域数据
        ZsyhAgentArea zsyhAgentAreaNew = new ZsyhAgentArea();
        zsyhAgentAreaNew.setAgentId(ids);
        List<ZsyhAgentArea> zsyhAgentAreaList = zsyhAgentAreaMapper.selectZsyhAgentAreaList(zsyhAgentAreaNew);
        rebackSysDtArea(zsyhAgentAreaList);
        zsyhAgentInfoMapper.deleteZsyhAgentAreaByAgentIds(Convert.toStrArray(ids));
        return zsyhAgentInfoMapper.deleteZsyhAgentInfoByIds(Convert.toStrArray(ids));
    }

    public void rebackSysDtArea(List<ZsyhAgentArea> zsyhAgentAreaList){
        String areaIds="";
        String cityAreaIds="";
        if(zsyhAgentAreaList.size()>0){
            for (ZsyhAgentArea zsyhAgentArea : zsyhAgentAreaList)
            {
                areaIds +=zsyhAgentArea.getAgentDistrict()+",";
                cityAreaIds += zsyhAgentArea.getAgentCity()+",";
            }
            //修改区域是否显示
            sysDtAreaMapper.updateSysDtAreaByIdsReverse(Convert.toStrArray(areaIds.substring(0,areaIds.length()-1)));
            //修改城市数量
            String[] cityIds = Convert.toStrArray(",",cityAreaIds);
            //去掉重复的城市数据
            Arrays.sort(cityIds);
            String[] afterCityIds= new String[cityIds.length];
            int index =1;
            afterCityIds[0]=cityIds[0];
            for(int i=1;i<cityIds.length;i++){
                if(!cityIds[i].equals(cityIds[i-1])){
                    afterCityIds[index++] = cityIds[i];
                }
            }
            String[] cityIdsNew = new String[index];
            System.arraycopy(afterCityIds,0,cityIdsNew,0,index);
            for(String cityId : cityIdsNew){
                SysDtArea cityArea = sysDtAreaMapper.selectSysDtAreaById(cityId);
                if(cityArea.getDistrictNum() == 0){
                    //更新省级城市数减+1
                    sysDtAreaMapper.updateSysDtAreaCityNumReverse(cityArea.getAreaParentId());
                }
            }
            //修改区域数量
            for (ZsyhAgentArea zsyhAgentArea : zsyhAgentAreaList)
            {
                //修改城市下区域的数量减-1
                sysDtAreaMapper.updateSysDtAreaNumByIdsReverse(Convert.toStrArray(zsyhAgentArea.getAgentCity()));
            }
            //更换店铺所属大区
            String[] areaIdsArray = areaIds.substring(0,areaIds.length()-1).split(",");
            zsyhUserRepairstoreMapper.updateStoreAgent(areaIdsArray);
        }
    }


    /**
     * 删除大区会长档案信息
     * 
     * @param id 大区会长档案ID
     * @return 结果
     */
    @Override
    public int deleteZsyhAgentInfoById(String id)
    {
        zsyhAgentInfoMapper.deleteZsyhAgentAreaByAgentId(id);
        return zsyhAgentInfoMapper.deleteZsyhAgentInfoById(id);
    }

    /**
     * 新增大区会长管辖区域信息
     * 
     * @param zsyhAgentInfo 大区会长档案对象
     */
    public void insertZsyhAgentArea(ZsyhAgentInfo zsyhAgentInfo)
    {
        String areaIds ="";
        String cityAreaIds ="";
        List<ZsyhAgentArea> zsyhAgentAreaList = zsyhAgentInfo.getZsyhAgentAreaList();
        String id = zsyhAgentInfo.getId();
        if (StringUtils.isNotNull(zsyhAgentAreaList))
        {
            List<ZsyhAgentArea> list = new ArrayList<ZsyhAgentArea>();
            for (ZsyhAgentArea zsyhAgentArea : zsyhAgentAreaList)
            {
                zsyhAgentArea.setId(IdWorker.getId());
                zsyhAgentArea.setAgentId(id);
                zsyhAgentArea.setCreateTime(DateUtils.getNowDate());
                zsyhAgentArea.setUpdateTime(DateUtils.getNowDate());
                list.add(zsyhAgentArea);
                areaIds +=zsyhAgentArea.getAgentDistrict()+",";
                cityAreaIds += zsyhAgentArea.getAgentCity()+",";
                //修改城市下区域的数量减-1
                sysDtAreaMapper.updateSysDtAreaNumByIds(Convert.toStrArray(zsyhAgentArea.getAgentCity()));
            }
            //更新店铺所属大区
            zsyhUserRepairstoreMapper.updateStoreAgentNew(areaIds.substring(0,areaIds.length()-1).split(","),id);
            if (list.size() > 0)
            {
                zsyhAgentInfoMapper.batchZsyhAgentArea(list);
                //修改区域是否显示
                sysDtAreaMapper.updateSysDtAreaByIds(Convert.toStrArray(areaIds.substring(0,areaIds.length()-1)));

                String[] cityIds = Convert.toStrArray(",",cityAreaIds);
                //去掉重复的城市数据
                Arrays.sort(cityIds);
                String[] afterCityIds= new String[cityIds.length];
                int index =1;
                afterCityIds[0]=cityIds[0];
                for(int i=1;i<cityIds.length;i++){
                    if(!cityIds[i].equals(cityIds[i-1])){
                        afterCityIds[index++] = cityIds[i];
                    }
                }
                String[] cityIdsNew = new String[index];
                System.arraycopy(afterCityIds,0,cityIdsNew,0,index);
                for(String cityId : cityIdsNew){
                    SysDtArea cityArea = sysDtAreaMapper.selectSysDtAreaById(cityId);
                    if(cityArea.getDistrictNum() == 0){
                        //更新省级城市数减-1
                        sysDtAreaMapper.updateSysDtAreaCityNum(cityArea.getAreaParentId());
                    }
                }
            }
        }
    }
}
